﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Application.Command.Add;
using Traceability.App.Domain;
using Traceability.App.ErrorCode;
using Traceability.App.Instructure;

namespace Traceability.App.Api.Application.Handler.Add
{
    public class UpdateBaoHandler : IRequestHandler<UpdateBaoCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<HealthreportModel> healthrrportRepo;
        private readonly IMapper mapper;

        public  UpdateBaoHandler(IBaseRepository<HealthreportModel> healthrrportRepo,IMapper mapper) 
        {
            this.healthrrportRepo = healthrrportRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(UpdateBaoCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var bao = healthrrportRepo.GetAll().Where(x => x.Id == request.Id).FirstOrDefault();
            bao = mapper.Map(request, bao);
            bao.UpdateTime=DateTime.Now;
            bao.UpdateName = "张三";
            bao.CreateName = "张三";
            bao.CreateTime = DateTime.Now;
            var i = healthrrportRepo.Update(bao);
            if (i>0)
            {
                res.Data = i;
                res.Code = ApiEnum.Success;
                res.Msg = "修改保健信息成功";
            }
            else
            {
                res.Data = i;
                res.Code = ApiEnum.Error;
                res.Msg = "修改保健信息失败";
            }
            return Task.FromResult(res);
        }
    }
}
